﻿var XMath = {
    // 取，v1和v2的最大值
    max: function (v1, v2) {
        if (v1 > v2) {
            return v1;
        }
        return v2;
    },
    // 取，v1和v2的最小值
    min: function (v1, v2) {
        if (v1 < v2) {
            return v1;
        }
        return v2;
    }
};
var LinkUtil = {
    // 计算路径
    // @from:XNode
    // @to:XNode
    // @return:string '0,0 1,1 2,2'
    calcPath: function (from, to) {
        var points = new Array();

        // 延长距离
        var extension = 10;

        // 寻找连个node中心点连接线的与各自边界的交点
        var p1 = from.getRootShape().selectLinkPoint(to.getCenter(), 1);
        var p2 = to.getRootShape().selectLinkPoint(from.getCenter(), 0);

        var c1 = from.getCenter();
        var c2 = to.getCenter();

        var rect1 = from.getAbsoluteRect();
        var rect2 = to.getAbsoluteRect();

        points.push(p1.pos);
        if (p1.dir == 'E') {
            if (p2.dir == 'N') {
                var p1extension = { x: p1.pos.x + extension, y: p1.pos.y };
                var p2extension = { x: p2.pos.x, y: p2.pos.y - extension };
                // 先到延长点
                points.push(p1extension);
                if (p2.pos.y > p1.pos.y) {
                    if (rect2.l > rect1.r) {
                        if (p2extension.y < p1.pos.y) {
                            var cx = rect1.r + (rect2.l - rect1.r) / 2;
                            var cy = p1extension.y + (p2extension.y - p1extension.y) / 2;
                            points.push({ x: cx, y: p1extension.y });
                            points.push({ x: cx, y: p2extension.y });
                        } else {
                            points.push({ x: p2extension.x, y: p1extension.y });
                        }
                    }
                } else {
                    if (rect2.l > rect1.r) {
                        var cx = rect1.r + (rect2.l - rect1.r) / 2;
                        var cy = p1extension.y + (p2extension.y - p1extension.y) / 2;
                        points.push({ x: cx, y: p1extension.y });
                        points.push({ x: cx, y: p2extension.y });
                    } else {
                        var cx = XMath.max(p1extension.x, rect2.r + extension);
                        points.push({ x: cx, y: p1extension.y });
                        points.push({ x: cx, y: p2extension.y });
                    }
                }
                points.push(p2extension);
            }
        } else if (p1.dir == 'S') {
            if (p2.dir == 'N') {
                var p1extension = { x: p1.pos.x, y: p1.pos.y + extension };
                var p2extension = { x: p2.pos.x, y: p2.pos.y - extension };
                var cx = p1extension.x + (p2extension.x - p1extension.x) / 2;
                var cy = p1extension.y + (p2extension.y - p1extension.y) / 2;
                // 先到延长点
                points.push(p1extension);
                if (p2.pos.y > p1.pos.y) {
                    // 从下接到目标的上面，直接往下走
                    points.push({ x: p1.pos.x, y: cy });
                    points.push({ x: p2.pos.x, y: cy });
                } else {
                    points.push({ x: cx, y: p1extension.y });
                    points.push({ x: cx, y: p2extension.y });
                }
                points.push(p2extension);
            }
        } else if (p1.dir == 'W') {
            if (p2.dir == 'N') {
                var p1extension = { x: p1.pos.x - extension, y: p1.pos.y };
                var p2extension = { x: p2.pos.x, y: p2.pos.y - extension };
                // 先到延长点
                points.push(p1extension);
                if (p2.pos.y > p1.pos.y) {
                    if (rect1.l > rect2.r) {
                        if (p2extension.y < p1.pos.y) {
                            var cx = rect1.l + (rect2.r - rect1.l) / 2;
                            var cy = p1extension.y + (p2extension.y - p1extension.y) / 2;
                            points.push({ x: cx, y: p1extension.y });
                            points.push({ x: cx, y: p2extension.y });
                        } else {
                            points.push({ x: p2extension.x, y: p1extension.y });
                        }
                    }
                } else {
                    if (rect1.l > rect2.r) {
                        var cx = rect1.l + (rect2.r - rect1.l) / 2;
                        var cy = p1extension.y + (p2extension.y - p1extension.y) / 2;
                        points.push({ x: cx, y: p1extension.y });
                        points.push({ x: cx, y: p2extension.y });
                    } else {
                        var cx = XMath.min(p1extension.x, rect2.l - extension);
                        points.push({ x: cx, y: p1extension.y });
                        points.push({ x: cx, y: p2extension.y });
                    }
                }
                points.push(p2extension);
            }
        }

        points.push(p2.pos);
        return points;
    }
};

var XPathUtil = {
    // 计算路径
    // @from:XNode
    // @to:XNode
    // @return:string '0,0 1,1 2,2'
    nodeToNode: function (from, to) {
        var points = new Array();

        // 延长距离
        var extension = 10;

        // 寻找连个node中心点连接线的与各自边界的交点
        var p1 = from.getRootShape().selectLinkPoint(to.getCenter(), 1);
        var p2 = to.getRootShape().selectLinkPoint(from.getCenter(), 0);

        var c1 = from.getCenter();
        var c2 = to.getCenter();

        var rect1 = from.getAbsoluteRect();
        var rect2 = to.getAbsoluteRect();

        points.push(p1.pos);
        if (p1.dir == 'E') {
            if (p2.dir == 'N') {
                var p1extension = { x: p1.pos.x + extension, y: p1.pos.y };
                var p2extension = { x: p2.pos.x, y: p2.pos.y - extension };
                // 先到延长点
                points.push(p1extension);
                if (p2.pos.y > p1.pos.y) {
                    if (rect2.l > rect1.r) {
                        if (p2extension.y < p1.pos.y) {
                            var cx = rect1.r + (rect2.l - rect1.r) / 2;
                            var cy = p1extension.y + (p2extension.y - p1extension.y) / 2;
                            points.push({ x: cx, y: p1extension.y });
                            points.push({ x: cx, y: p2extension.y });
                        } else {
                            points.push({ x: p2extension.x, y: p1extension.y });
                        }
                    }
                } else {
                    if (rect2.l > rect1.r) {
                        var cx = rect1.r + (rect2.l - rect1.r) / 2;
                        var cy = p1extension.y + (p2extension.y - p1extension.y) / 2;
                        points.push({ x: cx, y: p1extension.y });
                        points.push({ x: cx, y: p2extension.y });
                    } else {
                        var cx = XMath.max(p1extension.x, rect2.r + extension);
                        points.push({ x: cx, y: p1extension.y });
                        points.push({ x: cx, y: p2extension.y });
                    }
                }
                points.push(p2extension);
            }
        } else if (p1.dir == 'S') {
            if (p2.dir == 'N') {
                var p1extension = { x: p1.pos.x, y: p1.pos.y + extension };
                var p2extension = { x: p2.pos.x, y: p2.pos.y - extension };
                var cx = p1extension.x + (p2extension.x - p1extension.x) / 2;
                var cy = p1extension.y + (p2extension.y - p1extension.y) / 2;
                // 先到延长点
                points.push(p1extension);
                if (p2.pos.y > p1.pos.y) {
                    // 从下接到目标的上面，直接往下走
                    points.push({ x: p1.pos.x, y: cy });
                    points.push({ x: p2.pos.x, y: cy });
                } else {
                    points.push({ x: cx, y: p1extension.y });
                    points.push({ x: cx, y: p2extension.y });
                }
                points.push(p2extension);
            }
        } else if (p1.dir == 'W') {
            if (p2.dir == 'N') {
                var p1extension = { x: p1.pos.x - extension, y: p1.pos.y };
                var p2extension = { x: p2.pos.x, y: p2.pos.y - extension };
                // 先到延长点
                points.push(p1extension);
                if (p2.pos.y > p1.pos.y) {
                    if (rect1.l > rect2.r) {
                        if (p2extension.y < p1.pos.y) {
                            var cx = rect1.l + (rect2.r - rect1.l) / 2;
                            var cy = p1extension.y + (p2extension.y - p1extension.y) / 2;
                            points.push({ x: cx, y: p1extension.y });
                            points.push({ x: cx, y: p2extension.y });
                        } else {
                            points.push({ x: p2extension.x, y: p1extension.y });
                        }
                    }
                } else {
                    if (rect1.l > rect2.r) {
                        var cx = rect1.l + (rect2.r - rect1.l) / 2;
                        var cy = p1extension.y + (p2extension.y - p1extension.y) / 2;
                        points.push({ x: cx, y: p1extension.y });
                        points.push({ x: cx, y: p2extension.y });
                    } else {
                        var cx = XMath.min(p1extension.x, rect2.l - extension);
                        points.push({ x: cx, y: p1extension.y });
                        points.push({ x: cx, y: p2extension.y });
                    }
                }
                points.push(p2extension);
            }
        }

        points.push(p2.pos);
        return points;
    },
    // 计算路径
    // @from:XNode
    // @toPos:Point
    // @return:Point[]
    nodeToPos: function (from, toPos) {
        var points = new Array();

        // 延长距离
        var extension = 10;

        // 寻找连个node中心点连接线的与各自边界的交点
        var p1 = from.getRootShape().selectLinkPoint(toPos, 1);
        var p2 = { dir: 'N', pos: toPos };

        var c1 = from.getCenter();
        var c2 = toPos;

        var rect1 = from.getAbsoluteRect();
        var rect2 = { l: toPos.x - 5, t: toPos.y - 5, r: toPos.x + 10, b: toPos.y + 10 };

        points.push(p1.pos);
        if (p1.dir == 'E') {
            if (p2.dir == 'N') {
                var p1extension = { x: p1.pos.x + extension, y: p1.pos.y };
                var p2extension = { x: p2.pos.x, y: p2.pos.y - extension };
                // 先到延长点
                points.push(p1extension);
                if (p2.pos.y > p1.pos.y) {
                    if (rect2.l > rect1.r) {
                        if (p2extension.y < p1.pos.y) {
                            var cx = rect1.r + (rect2.l - rect1.r) / 2;
                            var cy = p1extension.y + (p2extension.y - p1extension.y) / 2;
                            points.push({ x: cx, y: p1extension.y });
                            points.push({ x: cx, y: p2extension.y });
                        } else {
                            points.push({ x: p2extension.x, y: p1extension.y });
                        }
                    }
                } else {
                    if (rect2.l > rect1.r) {
                        var cx = rect1.r + (rect2.l - rect1.r) / 2;
                        var cy = p1extension.y + (p2extension.y - p1extension.y) / 2;
                        points.push({ x: cx, y: p1extension.y });
                        points.push({ x: cx, y: p2extension.y });
                    } else {
                        var cx = XMath.max(p1extension.x, rect2.r + extension);
                        points.push({ x: cx, y: p1extension.y });
                        points.push({ x: cx, y: p2extension.y });
                    }
                }
                points.push(p2extension);
            }
        } else if (p1.dir == 'S') {
            if (p2.dir == 'N') {
                var p1extension = { x: p1.pos.x, y: p1.pos.y + extension };
                var p2extension = { x: p2.pos.x, y: p2.pos.y - extension };
                var cx = p1extension.x + (p2extension.x - p1extension.x) / 2;
                var cy = p1extension.y + (p2extension.y - p1extension.y) / 2;
                // 先到延长点
                points.push(p1extension);
                if (p2.pos.y > p1.pos.y) {
                    // 从下接到目标的上面，直接往下走
                    points.push({ x: p1.pos.x, y: cy });
                    points.push({ x: p2.pos.x, y: cy });
                } else {
                    points.push({ x: cx, y: p1extension.y });
                    points.push({ x: cx, y: p2extension.y });
                }
                points.push(p2extension);
            }
        } else if (p1.dir == 'W') {
            if (p2.dir == 'N') {
                var p1extension = { x: p1.pos.x - extension, y: p1.pos.y };
                var p2extension = { x: p2.pos.x, y: p2.pos.y - extension };
                // 先到延长点
                points.push(p1extension);
                if (p2.pos.y > p1.pos.y) {
                    if (rect1.l > rect2.r) {
                        if (p2extension.y < p1.pos.y) {
                            var cx = rect1.l + (rect2.r - rect1.l) / 2;
                            var cy = p1extension.y + (p2extension.y - p1extension.y) / 2;
                            points.push({ x: cx, y: p1extension.y });
                            points.push({ x: cx, y: p2extension.y });
                        } else {
                            points.push({ x: p2extension.x, y: p1extension.y });
                        }
                    }
                } else {
                    if (rect1.l > rect2.r) {
                        var cx = rect1.l + (rect2.r - rect1.l) / 2;
                        var cy = p1extension.y + (p2extension.y - p1extension.y) / 2;
                        points.push({ x: cx, y: p1extension.y });
                        points.push({ x: cx, y: p2extension.y });
                    } else {
                        var cx = XMath.min(p1extension.x, rect2.l - extension);
                        points.push({ x: cx, y: p1extension.y });
                        points.push({ x: cx, y: p2extension.y });
                    }
                }
                points.push(p2extension);
            }
        }

        points.push(p2.pos);
        return points;
    }
};

var XUtil = {
    getElementLeft: function (element) {
        var actualLeft = element.offsetLeft;
        var current = element.offsetParent;
        while (current != null) {
            actualLeft += current.offsetLeft;
            current = current.offsetParent;
        }
        return actualLeft;
    },
    getElementTop: function (element) {
        var actualTop = element.offsetTop;
        var current = element.offsetParent;
        while (current != null) {
            actualTop += current.offsetTop;
            current = current.offsetParent;
        }
        return actualTop;
    },
    // 判断point是否在element内
    pointInElement: function (pointInWindow, element) {
        var absoluteLeft = this.getElementLeft(element);
        var absoluteTop = this.getElementTop(element);
        var width = element.offsetWidth;
        var height = element.offsetHeight;
        if (pointInWindow.x >= absoluteLeft
            && pointInWindow.y >= absoluteTop
            && pointInWindow.x <= absoluteLeft + width
            && pointInWindow.y <= absoluteTop + height) {
            return true;
        }
        return false;
    }
}